package com.zengqingfa.springboot.redis.rest;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import com.zengqingfa.springboot.redis.entity.DictionaryItem;
import com.zengqingfa.springboot.redis.mapper.DictionaryItemMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @fileName: RedisController
 * @author: zengqf3
 * @date: 2021-3-5 10:24
 * @description:
 */
@RestController
@RequestMapping("/dictionary")
@Slf4j
public class DictionaryController {

    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private DictionaryItemMapper dictionaryItemMapper;


    /**
     * 使用redis中的数据类型String字符串来保存
     * @param tenantCode
     * @param applicationCode
     * @param dictCode
     * @return
     */
    @GetMapping("/getDictionaryItemsUseString")
    public List<DictionaryItem> getDictionaryItemsUseString(String tenantCode, String applicationCode, String dictCode) {
        String cacheKey = "DICT:" + tenantCode + ":" + applicationCode + ":" + dictCode;
        //获取值
        Object o = redisTemplate.opsForValue().get(cacheKey);
        List<DictionaryItem> dictionaryItems;
        if (Objects.isNull(o)) {
            //从数据库获取
            dictionaryItems = dictionaryItemMapper.selectList(new LambdaQueryWrapper<DictionaryItem>().eq(DictionaryItem::getDictCode, dictCode));
            //设值到redis中
            if (CollectionUtil.isEmpty(dictionaryItems)) {
                return Lists.newArrayList();
            }
            //设置值
            redisTemplate.opsForValue().set(cacheKey, dictionaryItems);
        } else {
            dictionaryItems = (List<DictionaryItem>) o;
        }
        return dictionaryItems;
    }

    /**
     * 使用redis中的List数据类型来存储
     * @param tenantCode
     * @param applicationCode
     * @param dictCode
     * @return
     */
    @GetMapping("/getDictionaryItemsUseList")
    public List<DictionaryItem> getDictionaryItemsUseList(String tenantCode, String applicationCode, String dictCode) {
        String cacheKey = "DICT:" + tenantCode + ":" + applicationCode + ":" + dictCode;
        //获取值
        List<DictionaryItem> dictionaryItems = redisTemplate.opsForList().range(cacheKey,0,-1);
        if (CollectionUtil.isEmpty(dictionaryItems)) {
            //从数据库获取
            dictionaryItems = dictionaryItemMapper.selectList(new LambdaQueryWrapper<DictionaryItem>().eq(DictionaryItem::getDictCode, dictCode));
            //设值到redis中
            if (CollectionUtil.isEmpty(dictionaryItems)) {
                return Lists.newArrayList();
            }
            //设置值
            redisTemplate.opsForList().rightPushAll(cacheKey, dictionaryItems);
        }
        return dictionaryItems;
    }

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 使用redis中的数据类型String字符串来保存
     * @param tenantCode
     * @param applicationCode
     * @param dictCode
     * @return
     */
    @GetMapping("/getDictionaryItemsUseString2")
    public List<DictionaryItem> getDictionaryItemsUseString2(String tenantCode, String applicationCode, String dictCode) {
        String cacheKey = "DICT:" + tenantCode + ":" + applicationCode + ":" + dictCode;
        //获取值
        String s = stringRedisTemplate.opsForValue().get(cacheKey);
        List<DictionaryItem> dictionaryItems;
        if (StringUtils.isBlank(s)) {
            //从数据库获取
            dictionaryItems = dictionaryItemMapper.selectList(new LambdaQueryWrapper<DictionaryItem>().eq(DictionaryItem::getDictCode, dictCode));
            //设值到redis中
            if (CollectionUtil.isEmpty(dictionaryItems)) {
                return Lists.newArrayList();
            }
            //设置值
            stringRedisTemplate.opsForValue().set(cacheKey, JSON.toJSONString(dictionaryItems));
        } else {
            dictionaryItems =JSON.parseArray(s,DictionaryItem.class);
        }
        return dictionaryItems;
    }

    @GetMapping("/deleteRegionsByParentCode")
    public Boolean deleteDictionaryItemByDictCode(String tenantCode, String applicationCode, String dictCode) {
        String cacheKey = "DICT:" + tenantCode + ":" + applicationCode + ":" + dictCode;
        //删除对应的key
        Boolean delete = redisTemplate.delete(cacheKey);
        return delete;
    }
}
